package com.xs.shop.marketing.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xs.shop.common.exceptions.BusinessException;
import com.xs.shop.common.exceptions.enums.Resp;
import com.xs.shop.marketing.model.entity.Activity;
import com.xs.shop.marketing.service.handlers.BaseContextHandler;
import com.xs.shop.marketing.service.mapper.ActivityMapper;
import com.xs.shop.marketing.service.service.ActivityService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xs
 * @since 2025-07-16
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Activity addActivity(Activity activity) {
        // 设置默认值
        if (activity.getStatus() == null) {
            activity.setStatus(0); // 默认未开始
        }
        activity.setCreatedAt(LocalDateTime.now());
        activity.setUpdatedAt(LocalDateTime.now());
        Long adminId = BaseContextHandler.get();
        if (adminId != null) {
            activity.setCreatedBy(adminId);
            activity.setUpdatedBy(adminId);
        }

        this.save(activity);
        return activity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteActivity(Long id) {
        if (id == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Activity updateActivity(Activity activity) {
        if (activity == null || activity.getId() == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        activity.setUpdatedAt(LocalDateTime.now());
        activity.setUpdatedBy(BaseContextHandler.get());
        this.updateById(activity);
        return activity;
    }

    @Override
    public Activity getActivityById(Long id) {
        if (id == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        return this.getById(id);
    }

    @Override
    public Page<Activity> pageActivity(Integer current, Integer pageSize,
                                      String name, Integer type, String startTime,
                                      String endTime, Integer status) {
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StringUtils.hasText(name), Activity::getName, name)
                .eq(type != null, Activity::getType, type)
                .ge(StringUtils.hasText(startTime), Activity::getStartTime, startTime)
                .le(StringUtils.hasText(endTime), Activity::getEndTime, endTime)
                .eq(status != null, Activity::getStatus, status);

        // 按创建时间倒序
        queryWrapper.orderByDesc(Activity::getCreatedAt);

        return this.page(new Page<>(current, pageSize), queryWrapper);
    }
}
